// 格式化时间等
// 年月时分秒
export const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
// dzl
export const formatDay = date => {
  if(date){
    const newdate = date.split(' ')[0]
    const time = date.split(' ')[1]
    return newdate.substring(5) + ' ' + time.substring(0,5)
  }
}
// 年月日
export const formatDate = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()

  return [year, month, day].map(formatNumber).join('/')
}
// 管理全局路由
export const setRouterConfig = (config, path) => {
  config.forEach((item) => {
    item.active = item.pagePath === path ? true : false
  })
  return config
}
export const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
/**
 * 转换录音时间戳
 */
export const timestamp = time => {
  if (typeof time !== 'number' || time < 0) {
    return time
  }
  let duration = Math.floor(time / 1000)
  if (time > 60000) {
    var minute = parseInt(duration / 60)
    duration = duration % 60
    var second = duration
    return ([minute, second]).map(function (n) {
      n = n.toString()
      return n[1] ? n : '0' + n
    }).join(':')
  } else {
    return Math.floor(time / 1000)
  }
}
export default {
  formatTime: formatTime,
  formatDate: formatDate,
  setRouterConfig: setRouterConfig
}